package com.ynet.middleground.riskmanage.utils;

import java.io.IOException;
import java.util.*;

import org.apache.commons.beanutils.BeanUtils;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;

/**
 * @author yangbh
 */
public class Map2BeanUtil {

    /**
     * 判断是否是基础数据类型的包装类型
     *
     * @param clz
     * @return
     */
    public static boolean isWrapClass(Class clz) {
        try {
            return ((Class)clz.getField("TYPE").get(null)).isPrimitive();
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 判断是否是基础数据类型，即 int,double,long等类似格式
     */
    public static boolean isCommonDataType(Class clazz) {
        return clazz.isPrimitive();
    }

    public static <T, K, V> T map2BeanBak(Map<K, V> mp, Class<T> beanCls) {
        Gson gson = new Gson();
        String tempJson = gson.toJson(mp);
        return gson.fromJson(tempJson, beanCls);
    }

    public static <T, K, V> T map2Bean(Map<K, V> mp, Class<T> beanCls) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        // 添加此配置，解决返回的 json 字符串中的某个键值对无法在实体类中找到对应属性的问题。即：多传递无用参数也不影响
        objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return objectMapper.readValue(JSONObject.toJSONString(mp), beanCls);
    }

    public static Map<?, ?> objectToMap(Object obj) throws Exception {
        if (obj == null) {
            return null;
        }
        if (obj instanceof Map) {
            return (Map)obj;
        }

        return BeanUtils.describe(obj);
    }

    private static List convetToLang(List list) throws Exception {

        List resultList = new ArrayList();

        for (Object o : list) {

            if (o instanceof List) {
                resultList.add(convetToLang((List)o));
                continue;
            }

            if (o instanceof Map) {
                resultList.add(convetToMap((Map)o));
                continue;
            }

            if (o.getClass().getName().contains("java.lang")) {
                resultList.add(o);
                continue;
            }

            resultList.add(BeanUtils.describe(o));
        }

        return resultList;
    }

    private static Map convetToMap(Map map) throws Exception {

        Map resultMap = new HashMap();

        Iterator iterator = map.keySet().iterator();

        while (iterator.hasNext()) {

            String next = (String)iterator.next();
            Object o = map.get(next);

            if (o instanceof List) {
                resultMap.put(next, convetToLang((List)o));
                continue;
            }

            if (o instanceof Map) {
                resultMap.put(next, convetToMap((Map)o));
                continue;
            }

            if (o.getClass().getName().contains("java.lang")) {
                resultMap.put(next, o);
                continue;
            }

            resultMap.put(next, BeanUtils.describe(o));

        }

        return resultMap;
    }

}
